$(function(){  
    // 配置jqGrid组件  
    $("#gridTable").jqGrid({  
        url: "get_all_workrequest.action",  
        datatype: "json",  
        mtype: "GET",  
        height: 350,  
        autowidth: true,  
        colModel: [  
              {name:"wrId",index:"wrId",label:"WRID",width:40},    
              {name:"wrName",index:"wrName",label:"WR Name",width:80,sortable:false},  
              {name:"status_key",index:"status_key",label:"Status",width:80,sortable:false}, 
              {name:"actual_hour",index:"actual_hour",label:"Actual Hours",width:120,sortable:false}  
        ],  
        viewrecords: true,  
        rowNum: 15,  
        rowList: [15,50,100],  
        prmNames: {search: "search"},   //(1)  
        jsonReader: {  
            root:"gridModel",       // (2)  
            records: "record",      // (3)  
            repeatitems : false     // (4)  
        },  
        pager: "#gridPager",  
        caption: "WR List",  
        hidegrid: false  
    }); 
    
    // 配置对话框  
    $("#consoleDlg").dialog({  
        autoOpen: false,      
        modal: true,    // 设置对话框为模态（modal）对话框  
        resizable: true,      
        width: 480,  
        buttons: {  // 为对话框添加按钮  
            "取消": function() {$("#consoleDlg").dialog("close")},  
            "创建": addContact,  
            "保存": updateContact,  
            "删除": deleteContact  
        }  
    });  
    
});

var openDialog4Adding = function() {  
    var consoleDlg = $("#consoleDlg");  
    var dialogButtonPanel = consoleDlg.siblings(".ui-dialog-buttonpane");  
    consoleDlg.find("input").removeAttr("disabled").val("");  
    dialogButtonPanel.find("button:not(:contains('取消'))").hide();  
    dialogButtonPanel.find("button:contains('创建')").show();  
    consoleDlg.dialog("option", "title", "创建新联系人").dialog("open");  
};  
var openDialog4Updating = function() {  
    var consoleDlg = $("#consoleDlg");  
    var dialogButtonPanel = consoleDlg.siblings(".ui-dialog-buttonpane");  
      
    consoleDlg.find("input").removeAttr("disabled");  
    dialogButtonPanel.find("button:not(:contains('取消'))").hide();  
    dialogButtonPanel.find("button:contains('保存')").show();  
    consoleDlg.dialog("option", "title", "修改联系人信息");  
      
    loadSelectedRowData();  
}  
var openDialog4Deleting = function() {  
    var consoleDlg = $("#consoleDlg");  
    var dialogButtonPanel = consoleDlg.siblings(".ui-dialog-buttonpane");  
      
    consoleDlg.find("input").attr("disabled", true);  
    dialogButtonPanel.find("button:not(:contains('取消'))").hide();  
    dialogButtonPanel.find("button:contains('删除')").show();  
    consoleDlg.dialog("option", "title", "删除联系人");  
      
    loadSelectedRowData();  
}  
var loadSelectedRowData = function() {    
    var selectedRowId = $("#gridTable").jqGrid("getGridParam", "selrow");  
    if (!selectedRowId) {  
        alert("请先选择需要编辑的行!");  
        return false;  
    } else {  
        var params = {  
            "contact.id" : selectedRowId  
        };  
        var actionUrl = "viewContact.action";  
        var $;
		// 从Server读取对应ID的JSON数据  
        $.ajax( {  
            url : actionUrl,  
            data : params,  
            dataType : "json",  
            cache : false,  
            error : function(textStatus, errorThrown) {  
                alert("系统ajax交互错误: " + textStatus);  
            },  
            success : function(data, textStatus) {  
                // 如果读取结果成功，则将信息载入到对话框中                   
                var rowData = data.contact;  
                var consoleDlg = $("#consoleDlg");  
                consoleDlg.find("#selectId").val(rowData.id);  
                consoleDlg.find("#lastName").val(rowData.lastName);  
                consoleDlg.find("#firstName").val(rowData.firstName);  
                consoleDlg.find("#certificateNo").val(rowData.certificateNo);  
                consoleDlg.find("#email").val(rowData.email);  
                consoleDlg.find("#telNo").val(rowData.telNo);  
                consoleDlg.find("#address").val(rowData.address);  
                consoleDlg.find("#idCardNo").val(rowData.idCardNo);  
                consoleDlg.find("#nationality").val(rowData.nationality);  
                  
                // 根据新载入的数据将表格中的对应数据行一并更新一下  
                var dataRow = {  
                        id : rowData.id,  
                        lastName : rowData.lastName,  
                        firstName : rowData.firstName,  
                        email : rowData.email,  
                        telNo : rowData.telNo  
                    };  
                  
                $("#gridTable").jqGrid("setRowData", data.contact.id, dataRow);  
                      
                // 打开对话框  
                consoleDlg.dialog("open");  
            }  
        });  
          
    }  
};  
var addContact = function() {  
    var consoleDlg = $("#consoleDlg");  
          
    var pLastName = $.trim(consoleDlg.find("#lastName").val());  
    var pFirstName = $.trim(consoleDlg.find("#firstName").val());  
    var pEmail = $.trim(consoleDlg.find("#email").val());  
    var pTelNo = $.trim(consoleDlg.find("#telNo").val());  
    var pAddress = $.trim(consoleDlg.find("#address").val());  
    var pIdCardNo = $.trim(consoleDlg.find("#idCardNo").val());  
    var pNationality = $.trim(consoleDlg.find("#nationality").val());  
      
    var params = {  
        "contact.lastName" : pLastName,  
        "contact.firstName" : pFirstName,  
        "contact.email" : pEmail,  
        "contact.telNo" : pTelNo,  
        "contact.address" : pAddress,  
        "contact.idCardNo" : pIdCardNo,  
        "contact.nationality" : pNationality  
    };  
      
    var actionUrl = "createContact.action"  
      
    $.ajax( {  
        url : actionUrl,  
        data : params,  
        dataType : "json",  
        cache : false,  
        error : function(textStatus, errorThrown) {  
            alert("系统ajax交互错误: " + textStatus);  
        },  
        success : function(data, textStatus) {  
            if(data.ajaxResult == "success") {  
                var dataRow = {  
                    id : data.contact.id,   // 从Server端得到系统分配的id  
                    lastName : pLastName,  
                    firstName : pFirstName,  
                    email : pEmail,  
                    telNo : pTelNo  
                };  
                  
                var srcrowid = $("#gridTable").jqGrid("getGridParam", "selrow");  
                  
                if(srcrowid) {  
                    $("#gridTable").jqGrid("addRowData", data.contact.id, dataRow, "before", srcrowid);  
                } else {  
                    $("#gridTable").jqGrid("addRowData", data.contact.id, dataRow, "first");  
                }  
                consoleDlg.dialog("close");  
                  
                alert("联系人添加操作成功!");  
                  
            } else {  
                alert("添加操作失败!");  
            }  
        }  
    });  
};  
var updateContact = function() {  
    var consoleDlg = $("#consoleDlg");  
      
    var pId = $.trim(consoleDlg.find("#selectId").val());  
    var pLastName = $.trim(consoleDlg.find("#lastName").val());  
    var pFirstName = $.trim(consoleDlg.find("#firstName").val());  
    var pEmail = $.trim(consoleDlg.find("#email").val());  
    var pTelNo = $.trim(consoleDlg.find("#telNo").val());  
    var pAddress = $.trim(consoleDlg.find("#address").val());  
    var pIdCardNo = $.trim(consoleDlg.find("#idCardNo").val());  
    var pNationality = $.trim(consoleDlg.find("#nationality").val());  
    var params = {  
        "contact.id" : pId,  
        "contact.lastName" : pLastName,  
        "contact.firstName" : pFirstName,  
        "contact.email" : pEmail,  
        "contact.telNo" : pTelNo,  
        "contact.address" : pAddress,  
        "contact.idCardNo" : pIdCardNo,  
        "contact.nationality" : pNationality  
    };  
    var actionUrl = "updateContact.action";  
    $.ajax( {  
        url : actionUrl,  
        data : params,  
        dataType : "json",  
        cache : false,  
        error : function(textStatus, errorThrown) {  
            alert("系统ajax交互错误: " + textStatus);  
        },  
        success : function(data, textStatus) {  
            if (data.ajaxResult == "success") {  
                var dataRow = {  
                    id : data.contact.id,  
                    lastName : pLastName,  
                    firstName : pFirstName,  
                    email : pEmail,  
                    telNo : pTelNo  
                };  
                $("#gridTable").jqGrid("setRowData", data.contact.id, dataRow, {color:"#FF0000"});  
                  
                alert("联系人信息更新成功!");  
                  
                consoleDlg.dialog("close");  
                  
            } else {  
                alert("修改操作失败!");  
            }  
        }  
    });  
};  
var deleteContact = function() {  
    var consoleDlg = $("#consoleDlg");  
      
    var pId = $.trim(consoleDlg.find("#selectId").val());  
    var params = {  
        "contact.id" : pId  
    };  
    var actionUrl = "deleteContact.action";  
    $.ajax({  
        url : actionUrl,  
        data : params,  
        dataType : "json",  
        cache : false,  
        error : function(textStatus, errorThrown) {  
            alert("系统ajax交互错误: " + textStatus);  
        },  
        success : function(data, textStatus) {  
            if (data.ajaxResult == "success") {  
                $("#gridTable").jqGrid("delRowData", pId);  
                  
                consoleDlg.dialog("close");  
                alert("联系人删除成功!");  
            } else {  
                alert("删除操作失败!");  
            }  
        }  
    });  
};  
